package io.binghe.concurrent.lab06;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 创建一个固定大小的线程池
 * @author CurleyG
 * @date 2025/3/25 23:46
 */
public class FixedThreadPoolExample {
    public static void main(String[] args) {
        // 创建一个固定大小的线程池，包含 3 个线程
        ExecutorService executor = Executors.newFixedThreadPool(3);

        // 提交 5 个任务
        for (int i = 1; i <= 5; i++) {
            final int taskId = i;
            executor.execute(() -> {
                System.out.println(Thread.currentThread().getName() + " 正在执行任务 " + taskId);
                try {
                    Thread.sleep(2000); // 模拟任务执行
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        executor.shutdown(); // 关闭线程池
    }
}
